package com.qc31.comlib.choose.team

import android.app.Activity
import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.rx.RxIo
import com.qc31.baselib.utils.isFastClick
import com.qc31.baselib.utils.loadGif
import com.qc31.baselib.utils.setResultTo
import com.qc31.baselib.utils.toast
import com.qc31.comlib.Keys
import com.qc31.comlib.R
import com.qc31.comlib.adapter.ChooseTeamAdapter
import com.qc31.comlib.databinding.FragmentChooseCarBinding
import com.qc31.comlib.databinding.IncludeChooseTeamCarBinding

/** @Author Jasper @Time 2021/5/14 15:59 @Desc */
class ChooseTeamFragment :
    LazyFragment<FragmentChooseCarBinding>(FragmentChooseCarBinding::inflate) {
    private var isMap: Boolean = false
    private var isChoose: Boolean = false

    private lateinit var mTeamVB: IncludeChooseTeamCarBinding

    private val mAdapter by lazy { ChooseTeamAdapter() }

    private val mViewModel by activityViewModels<ChooseTeamViewModel>()

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        if (activity is ChooseTeamActivity) {
            isMap = activity.intent.getBooleanExtra("isMap", false)
            isChoose = activity.intent.getBooleanExtra("isChoose", false)
            mAdapter.isChoose = isChoose
        }
    }

    //空布局
    private val emptyView by lazy {

        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.includeRecycler.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(
            requireContext(),
            R.mipmap.ic_empty
        )
        view
    }

    override fun initView(view: View) {
        mTeamVB = IncludeChooseTeamCarBinding.bind(binding.root)
        initRecyclerView()
    }

    override fun initData() {
        mViewModel.getAllTeam()
        mViewModel.toastObserver().observeOn(RxIo.mainScheduler())
            .autoDispose(scopeProvider).subscribe(::toastObserver)
        mViewModel.teamTitleSub.observeOn(RxIo.mainScheduler()).autoDispose(scopeProvider)
            .subscribe { mTeamVB.tvTeamName.text = it }

        mViewModel.teamsSub.hide().observeOn(RxIo.mainScheduler())
            .autoDispose(scopeProvider).subscribe {
                if (it.size <= 0) {
                    mAdapter.setEmptyView(emptyView)
                    return@subscribe
                }
                mAdapter.setList(it)
            }

        mViewModel.isRootTeam.observe(this) {
            mTeamVB.tvTeamBack.isVisible = !it
            mTeamVB.tvTeamHome.isVisible = !it
        }
    }

    override fun initEvent() {
        mTeamVB.tvTeamBack.setOnClickListener {
            mViewModel.previousTeam()
        }
        mTeamVB.tvTeamHome.setOnClickListener {
            mViewModel.rootTeam()
        }
    }


    private fun initRecyclerView() {
        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        mAdapter.setEmptyView(emptyView)
        binding.includeRecycler.recyclerView.adapter = mAdapter.apply {
            setOnItemClickListener { adapter, view, position ->
                mViewModel.getTeamsById(data[position].teamId)
            }
            setOnItemChildClickListener { adapter, view, position ->
                if (isFastClick()) return@setOnItemChildClickListener
                val teamInfo = data[position]
                if (!isChoose && teamInfo.carNum <= 0) {
                    R.string.toast_choose_no_car.toast(requireContext())
                    return@setOnItemChildClickListener
                }
                if (isMap && teamInfo.carNum > 500) {
                    R.string.toast_team_cars_much.toast(requireContext())
                    return@setOnItemChildClickListener
                }
                setResultTo(requireActivity()) {
                    putExtra(Keys.INTENT_TEAM_ID, teamInfo.teamId)
                    putExtra(Keys.INTENT_TEAM_NUM, teamInfo.carNum.toString())
                    putExtra(Keys.INTENT_TEAM_NAME, teamInfo.teamName)
                }
                requireActivity().finish()
            }
        }
    }
}